{include file="public/header" /}
<body>
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-tab-content">
            <form id="productCategoryForm" class="layui-form layui-form-pane" autocomplete="off">
                <div class="layui-form-item">
                    <label class="layui-form-label layui-required">上级分类</label>
                    <div class="layui-input-block">
                        <select name="pid" id="nodeId" lay-filter="nodeId" lay-search>
                            <option value="">请选择</option>
                            <option value="0">无上级</option>
                            {volist name="tree_list" id="vo"}
                            <option value="{$vo.id}">{$vo.name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <!--<div class="layui-form-item">
                    <div class="layui-upload">
                        <label class="layui-form-label layui-required">封面图</label>
                        <div class="layui-upload">
                            <div class="layui-input-block">
                                <div class="upload-frame" id="uploadCover">
                                    <i class="layui-icon layui-icon-addition"></i>
                                </div>
                                <div class="preview-box">
                                    <div class="uploader-list" id="coverPreview"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <input type="hidden" name="cover" id="cover">
                </div>-->

                <div class="layui-form-item">
                    <label class="layui-form-label layui-required">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="productCategoryName" class="layui-input" placeholder="请输入分类名称">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">URL名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="seo_link" id="categoryProductSeoLink" class="layui-input" placeholder="自定义分类URL名称，只允许输入英文字母、数字、- 和 _">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="text" name="sort" id="sort" value="255" class="layui-input" placeholder="请输入排序，数字越小，排序越靠前">
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="status" id="status" checked lay-text="显示|显示" lay-skin="switch">
                        <input type="checkbox" name="is_recommended" id="isRecommended" lay-text="推荐|推荐" lay-skin="switch">
                    </div>
                </div>

                <div class="btn-block">
                    <button type="button" onclick="sendProductCategoryForm()" class="layui-btn layui-btn-normal">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
</body>

<script>
    layui.use(['form','upload'],function () {
        var $ = layui.jquery,
            form = layui.form,
            upload = layui.upload

        // 封面
        uploadTemplate('#uploadCover','#coverPreview','#cover','cover','productCoverLoad')

        function uploadTemplate(btn,preview,value,flag,load){
            upload.render({
                elem: btn,
                url: "{:url('section/upload')}",
                multiple: false,
                accept: 'file',
                size: uploadFileSize,
                acceptMime: uploadImgExt,
                before: function (obj) {
                    $(btn).addClass('upload-btn-hide')
                    $(preview).empty()
                    load = layer.load(1, {shade: [0.5, '#FFF'], time: 0})//弹出遮罩层
                },
                done: function (res) {
                    layer.close(load)//关闭遮罩层
                    sucMsg(res.msg)
                    if (res.code == 'suc') {
                        $(preview).append(
                            '<div class="file-items">' +
                            '<div class="handle" data-flag="' + flag + '" data-path="' + res.url + '"><i class="layui-icon layui-icon-delete"></i></div>' +
                            '<img class="cover-preview" src="' + res.url + '">' +
                            '</div>'
                        );

                        $(value).val(res.url)
                    } else {
                        $(btn).removeClass('upload-btn-hide')
                    }
                }
            })
        }
    })

    $(document).on("click", ".file-items .handle", function (event) {
        let flag = $(this).attr('data-flag')

        $('#uploadCover').removeClass('upload-btn-hide')
        $("#cover").val('')

        $(this).parent().remove();
    })

    function sendProductCategoryForm() {
        let pid = $("#nodeId").val()
        let name = $("#productCategoryName").val()
        if (emptyCheck(pid)){
            hintMsg('请选择上级分类','nodeId')
            return false
        }else if(emptyCheck(name)){
            hintMsg('请输入分类名称','productCategoryName')
            return false
        }
        //弹出遮罩层，防止多次点击
        var productCategoryAddLoad = layer.load(1,{shade:[0.5,'#FFF'],time:0})
        let data = $("#productCategoryForm").serialize()
        ajaxRequest("{:url('categoryProduct/add')}",data,function (res) {
            if (res.code == 'suc'){
                sucMsg(res.msg,function () {
                    //刷新父级页面
                    xadmin.father_reload()
                    //关闭遮罩层
                    layer.close(productCategoryAddLoad)
                    //关闭当前弹出层
                    xadmin.close()
                })
            }else {
                errMsg(res.msg,function () {
                    //关闭遮罩层
                    layer.close(productCategoryAddLoad)
                })
            }
        })
    }
</script>